<template>
  <div>
    <el-dialog :visible.sync="dialogVisible" width="600px"
               :close-on-click-modal="false"
               :close-on-press-escape='false'
               :title="form.id?'编辑规则':'新增规则'" center>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="执行对象" prop="executeObId">
          <el-input v-model="form.executeObId"></el-input>
        </el-form-item>
        <el-form-item label="归档文件夹" prop="temailFolderId">
          <el-input v-model="form.temailFolderId" placeholder="输入关键字进行过滤"></el-input>
        </el-form-item>
        <el-form-item label="规则" prop="ruleEmailAccount">
          <el-input v-model="form.ruleEmailAccount"></el-input>
        </el-form-item>
      </el-form>
      <!-- 底部按钮-->
      <div slot="footer" class="dialog-footer" align="right">
        <el-button size="mini" @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" size="mini" @click="submit">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
    import {addRules, updateRules} from "@/api/mail/admin/mail-doc";

    export default {
        name: "MailDocRules",
        data() {
            return {
                folderList: [],
                dialogVisible: false,
                form: {
                    id: '',
                    executeObId: '',//执行对象
                    ruleEmailAccount: '',//账户
                    temailFolderId: ''//文件夹
                },
                rules: {
                    executeObId: [
                        {required: true, message: '请输入执行对象', trigger: 'blur'},
                    ],
                    ruleEmailAccount: [
                        {required: true, message: '请输入规则邮箱', trigger: 'blur'},
                    ],
                    temailFolderId: [
                        {required: true, message: '请选择归档文件夹', trigger: 'blur'},
                    ]
                },
                defaultProps: {
                    children: 'children',
                    label: 'name'
                }
            }
        },
        watch: {
            filterText(val) {
                this.$refs.tree.filter(val);
            }
        },
        methods: {
            init(params) {
                if (JSON.stringify(params) !== '{}') {
                    this.form.id = params.id;
                    this.form.executeObId = params.executeObId;
                    this.form.ruleEmailAccount = params.ruleEmailAccount;
                    this.form.temailFolderId = params.temailFolderId;
                }
                this.dialogVisible = !this.dialogVisible;
            },
            //保存
            submit() {
                this.$refs.form.validate((valid) => {
                    if (valid) {
                        if (this.form.id) {
                            updateRules(this.form).then(res => {
                                this.dealEnd(res, 'form')
                            }).catch(err => {
                                console.log(err)
                            })
                        } else {
                            addRules(this.form).then(res => {
                                this.dealEnd(res, 'form')
                            }).catch(err => {
                                console.log(err)
                            })
                        }
                    } else {
                        return false;
                    }
                });
            },
            dealEnd(res, formName) {
                if (res.data.success) {
                    this.dialogVisible = false;
                    this.$refs[formName].resetFields();
                    this.$message({
                        message: res.data.msg,
                        type: 'success'
                    });
                } else {
                    this.$message({
                        type: "success",
                        message: res.data.msg
                    });
                }

            }
        }
    }
</script>

<style scoped>

</style>
